package com.ruoyi.report_sdj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sdj.domain.bo.SdjMglblbgBaDetailBo;
import com.ruoyi.report_sdj.domain.vo.SdjMglblbgBaDetailVo;
import com.ruoyi.report_sdj.domain.SdjMglblbgBaDetail;
import com.ruoyi.report_sdj.mapper.SdjMglblbgBaDetailMapper;
import com.ruoyi.report_sdj.service.ISdjMglblbgBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 锚杆拉拔力试验检测记录 详细Service业务层处理
 *
 * @author zyb
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class SdjMglblbgBaDetailServiceImpl implements ISdjMglblbgBaDetailService {

    private final SdjMglblbgBaDetailMapper baseMapper;

    /**
     * 查询锚杆拉拔力试验检测记录 详细
     */
    @Override
    public SdjMglblbgBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询锚杆拉拔力试验检测记录 详细列表
     */
    @Override
    public TableDataInfo<SdjMglblbgBaDetailVo> queryPageList(SdjMglblbgBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SdjMglblbgBaDetail> lqw = buildQueryWrapper(bo);
        Page<SdjMglblbgBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询锚杆拉拔力试验检测记录 详细列表
     */
    @Override
    public List<SdjMglblbgBaDetailVo> queryList(SdjMglblbgBaDetailBo bo) {
        LambdaQueryWrapper<SdjMglblbgBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SdjMglblbgBaDetail> buildQueryWrapper(SdjMglblbgBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SdjMglblbgBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, SdjMglblbgBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getXuh() != null, SdjMglblbgBaDetail::getXuh, bo.getXuh());
        lqw.eq(StringUtils.isNotBlank(bo.getMaogbh()), SdjMglblbgBaDetail::getMaogbh, bo.getMaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancwz()), SdjMglblbgBaDetail::getJiancwz, bo.getJiancwz());
        lqw.eq(bo.getLingq() != null, SdjMglblbgBaDetail::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getJisyq()), SdjMglblbgBaDetail::getJisyq, bo.getJisyq());
        lqw.eq(bo.getJiancjg() != null, SdjMglblbgBaDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), SdjMglblbgBaDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, SdjMglblbgBaDetail::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, SdjMglblbgBaDetail::getNum2, bo.getNum2());
        return lqw;
    }

    /**
     * 新增锚杆拉拔力试验检测记录 详细
     */
    @Override
    public Boolean insertByBo(SdjMglblbgBaDetailBo bo) {
        SdjMglblbgBaDetail add = BeanUtil.toBean(bo, SdjMglblbgBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改锚杆拉拔力试验检测记录 详细
     */
    @Override
    public Boolean updateByBo(SdjMglblbgBaDetailBo bo) {
        SdjMglblbgBaDetail update = BeanUtil.toBean(bo, SdjMglblbgBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SdjMglblbgBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除锚杆拉拔力试验检测记录 详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
